﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RukouMVC.Models;
using System.Text;

namespace RukouMVC.Controllers
{

    public class AllEntries
    {
        public List<CustomPrivateEntry> PrivateEntries { get; set; }
        public List<Entry> Entries { get; set; }
        public List<PrivateEntryCategory> PrivateEntryCategories { get; set; }
        public List<EntryImage> EntryImages { get; set; }
        public User user { get; set; }
        public string strOutputJS { get; set; }

    }

    public class CustomPrivateEntry
    {
        public int PrivateEntryId { get; set; }
        public string Name { get; set; }
        public string Icon { get; set; }
        public string Color { get; set; }
        public int? PrivateEntryCategoryId { get; set; }
        public string Url { get; set; }
        public string Description { get; set; }
        public string ImageSource { get; set; }

    }

    [HandleError]
    public class HomeController : BaseController
    {
        AllEntries allEntries = new AllEntries();

        public ActionResult Index_new()
        {
            ViewData["otherview"] = false;

            string s = cookie["Email"];
            if (cookie["UserId"] != null && cookie["UserId"] != string.Empty)
            {
                int id =  int.Parse(cookie["UserId"]);
                User user = rdc.Users.SingleOrDefault(u => u.UserId ==id);
                


                var peList = from pe in rdc.PrivateEntries
                             join peim in rdc.PrivateEntryImages on new { petnryid = pe.PrivateEntryId, stylename = user.Style.StyleName } equals new { petnryid = peim.PrivateEntryId, stylename = peim.Style1.StyleName }
                             into y from x in y.DefaultIfEmpty()
                             where pe.CreatedBy == id 
                             orderby pe.PrivateEntryCategoryId, pe.OrderInCategory
                             select new CustomPrivateEntry
                             {
                                 PrivateEntryId = pe.PrivateEntryId,
                                 Name = pe.Name,
                                 Icon = pe.Icon,
                                 Color = pe.Color,
                                 PrivateEntryCategoryId = pe.PrivateEntryCategoryId,
                                 Url = pe.Url,
                                 Description = pe.Description,
                                 ImageSource = x.ImageSource
                             };
                allEntries.PrivateEntries = peList.ToList();
                

                var query = from eim in rdc.EntryImages where eim.StyleId==user.StyleId  select eim;
                allEntries.EntryImages = query.ToList<EntryImage>();

                var categories = from pec in rdc.PrivateEntryCategories where pec.CreatedBy == int.Parse(cookie["UserId"]) select pec;
                allEntries.PrivateEntryCategories = categories.ToList();

                StringBuilder script = new StringBuilder();
                script.Append("<script type='text/javascript'>");
                script.Append("var categories ='{\"categories\":[");
                foreach (PrivateEntryCategory pec in categories)
                {
                    script.Append("\"" + pec.PrivateEntryCategoryId + "-" + pec.Name + "\",");
                }
                script.Append("]}';</script>");
                script.Replace(",]", "]");
                allEntries.strOutputJS = script.ToString();

               
                allEntries.user = user;
                user.LastLogin = DateTime.Now;
                rdc.SubmitChanges();
                if (user.Style != null)
                {
                    switch (user.Style.StyleName)
                    {
                        case "simple":
                            return View("Index_new", allEntries);
                        case "span":
                            return View("Index_span", allEntries);
                        case "round":
                            return View("Index_round", allEntries);
                        default:
                            return View("Index_new", allEntries);
                    }
                }
                else
                {
                    return View("Index_new", allEntries);
                }

                
            }
            else
            {
                var entries = (from e in rdc.Entries select e).Take(42);
                allEntries.Entries = entries.ToList();

                return View("Index_nologin", allEntries);
            }

            
            
        }


        public ActionResult RukouDetail(int id)
        {
            ViewData["otherview"] = true;

            User user = rdc.Users.Single(u => u.UserId == (id - 1234));
            allEntries.user = user;
            ViewData["DisplayName"] = user.DisplayName;

            var peList = from pe in rdc.PrivateEntries
                         join peim in rdc.PrivateEntryImages on new { petnryid = pe.PrivateEntryId, stylename = user.Style.StyleName } equals new { petnryid = peim.PrivateEntryId, stylename = peim.Style1.StyleName }
                         into y
                         from x in y.DefaultIfEmpty()
                         where pe.CreatedBy == (id - 1234)
                         orderby pe.PrivateEntryCategoryId, pe.OrderInCategory
                         select new CustomPrivateEntry
                         {
                             PrivateEntryId = pe.PrivateEntryId,
                             Name = pe.Name,
                             Icon = pe.Icon,
                             Color = pe.Color,
                             PrivateEntryCategoryId = pe.PrivateEntryCategoryId,
                             Url = pe.Url,
                             Description = pe.Description,
                             ImageSource = x.ImageSource
                         };
            allEntries.PrivateEntries = peList.ToList();


            var query = from eim in rdc.EntryImages select eim;
            allEntries.EntryImages = query.ToList<EntryImage>();

            var categories = from pec in rdc.PrivateEntryCategories where pec.CreatedBy == (id - 1234) select pec;
            allEntries.PrivateEntryCategories = categories.ToList();

            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.Append("categories ='{\"categories\":[");
            foreach (PrivateEntryCategory pec in categories)
            {
                script.Append("\"" + pec.PrivateEntryCategoryId + "-" + pec.Name + "\",");
            }
            script.Append("]}';</script>");
            script.Replace(",]", "]");
            allEntries.strOutputJS = script.ToString();


            if (user.Style != null)
            {
                switch (user.Style.StyleName)
                {
                    case "simple":
                        return View("Index_new", allEntries);
                    case "span":
                        return View("Index_span", allEntries);
                    default:
                        return View("Index_new", allEntries);
                }
            }
            else
            {
                return View("Index_new", allEntries);
            }
        }

        public ActionResult About()
        {
            return View();
        }
    }


    
}
